﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;

namespace Avatar.Company
{

    public static class InterViewRecordManager
    {
        public static bool AddInterViewRecord(string _company,
            string _resumeID,
            Guid _jobID,
            string _address,
            string _content
            )
        {
            #region 入口检测

            if (string.IsNullOrEmpty(_company)) throw new Exception("企业名称不能为空");
            if (string.IsNullOrEmpty(_resumeID)) throw new Exception("简历ID不能为空");
            if (string.IsNullOrEmpty(_address)) throw new Exception("面试地址不能为空");
            if (string.IsNullOrEmpty(_content)) throw new Exception("通知内容不能为空");
            if (_jobID == Guid.Empty) throw new Exception("工作ID不能为空");

            #endregion

            var _interview = DBHelper.Get<InterViewRecord>(iv => iv.Company == _company && iv.ResumeID == _resumeID && iv.JobID == _jobID,
                "获取面试通知记录出错:");

            if (_interview == null)
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    InterViewRecord _iv = new InterViewRecord();

                    _iv.Company = _company;
                    _iv.ResumeID = _resumeID;
                    _iv.JobID = _jobID;
                    _iv.Date = DateTime.Now;
                    _iv.Location = _address;
                    _iv.Content = _content;
                    _iv.InterviewDate = DateTime.Now;

                    _adc.InterViewRecord.InsertOnSubmit(_iv);
                    _adc.SubmitChanges();
                }
            }
            return true;
        }
        /// <summary>
        /// 获取面试通知记录信息
        /// </summary>
        /// <param name="_company"></param>
        /// <param name="_resumeID"></param>
        /// <param name="_jobID"></param>
        /// <returns></returns>
        public static InterViewRecord GetInterViewRecord(string _company, string _user, Guid _jobID)
        {
            return DBHelper.Get<InterViewRecord>(iv => iv.Company == _company && iv.ResumeID == _user && iv.JobID == _jobID, "获取面试通知记录信息出错:");
        }
        /// <summary>
        /// 删除面试通知记录
        /// </summary>
        /// <param name="_recordID"></param>
        public static void DeleteInterViewRecord(int _recordID)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _ivr = _adc.InterViewRecord.Where(iv=>iv.ID==_recordID).FirstOrDefault();
                if (_ivr != null)
                {
                    _adc.InterViewRecord.DeleteOnSubmit(_ivr);
                    _adc.SubmitChanges();
                }
            }
        }
    }
}
